package cn.cf.kxx;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.annotation.SuppressLint;
import android.app.ZysjSystemManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import cn.cf.kxx.data.DataSharedPreferences;
import cn.cf.kxx.fram.OnActivityInteractionListener;
import cn.cf.kxx.fram.OnFragmentInteractionListener;
import cn.cf.kxx.model.dev.Hold;
import cn.cf.kxx.service.IMyAidlCallBack;
import cn.cf.kxx.service.ISerialAidlInterface;
import cn.cf.kxx.service.SerialService;
import cn.cf.kxx.tools.Tools;

public class SettingActivity extends Fragment implements CompoundButton.OnCheckedChangeListener, TextWatcher, View.OnClickListener, OnFragmentInteractionListener, TextView.OnEditorActionListener {

    @Override
    public void onRefrashString(String type) {
    }

    @Override
    public void onRefrashValve(int status) {

    }
    Hold hold;
    @Override
    public void onRefrashValues(Object obj) {
        hold = (Hold) obj;

        id.setText(String.valueOf(hold.getId()));
        baud.setText(String.valueOf(hold.getBaud()));
        version.setText(String.valueOf(hold.getVersion()));
        mnl1.setText(String.valueOf(hold.getAd1()));
        mnl2.setText(String.valueOf(hold.getAd2()));


        wxxd1.setText(String.valueOf(hold.getXd1()));
        wxxd2.setText(String.valueOf(hold.getXd2()));
        wxid1.setText(String.valueOf(hold.getId1()));
        wxid2.setText(String.valueOf(hold.getId2()));
        wxks1.setText(String.valueOf(hold.getKx1()));
        wxks2.setText(String.valueOf(hold.getKx2()));
    }

    OnActivityInteractionListener onActivityInteractionListener;
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        onActivityInteractionListener = (OnActivityInteractionListener) context;
    }

    private ZysjSystemManager manager;

    Switch dhlyc,waterType,reset;

    EditText pmbg,deviceId,id,baud,version,mnl1,mnl2,wxxd1,wxxd2,wxid1,wxid2,wxks1,wxks2,devBhTxt;

    Button goHome,readSetting,writeSetting,writeId;


    View contentView;

    public static SettingActivity newInstance(Bundle args) {
        SettingActivity fragment = new SettingActivity();
        fragment.setArguments(args);
        return fragment;
    }

    String devBh;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            Bundle bundle = getArguments();
            devBh = bundle.getString("devBh");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (contentView == null) {
            contentView = inflater.inflate(R.layout.activity_setting, container, false);
        } else {
            ViewGroup parent = (ViewGroup) contentView.getParent();
            if (parent != null) {
                parent.removeView(contentView);
            }
        }
        manager = (ZysjSystemManager)getActivity().getSystemService("zysj");
        dhlyc = contentView.findViewById(R.id.dhlyc);
        dhlyc.setOnCheckedChangeListener(this);
        pmbg = contentView.findViewById(R.id.pmbg);
        pmbg.setText(String.valueOf(manager.zYgetBackLight()));
        pmbg.addTextChangedListener(this);
        waterType = contentView.findViewById(R.id.waterType);
        waterType.setOnCheckedChangeListener(this);
        reset = contentView.findViewById(R.id.reset);
        reset.setOnCheckedChangeListener(this);
        deviceId = contentView.findViewById(R.id.deviceId);
        deviceId.setText(Tools.getDeviceId(getActivity()));

        devBhTxt = contentView.findViewById(R.id.devBh);
        devBhTxt.setText(devBh);

        id = contentView.findViewById(R.id.id);
        baud = contentView.findViewById(R.id.baud);
        version = contentView.findViewById(R.id.version);
        mnl1 = contentView.findViewById(R.id.mnl1);
        mnl2 = contentView.findViewById(R.id.mnl2);
        wxxd1 = contentView.findViewById(R.id.wxxd1);
        wxxd2 = contentView.findViewById(R.id.wxxd2);
        wxid1 = contentView.findViewById(R.id.wxid1);
        wxid2 = contentView.findViewById(R.id.wxid2);
        wxks1 = contentView.findViewById(R.id.wxks1);
        wxks2 = contentView.findViewById(R.id.wxks2);

        id.setOnEditorActionListener(this);
        baud.setOnEditorActionListener(this);
        version.setOnEditorActionListener(this);
        mnl1.setOnEditorActionListener(this);
        mnl2.setOnEditorActionListener(this);
        wxxd1.setOnEditorActionListener(this);
        wxxd2.setOnEditorActionListener(this);
        wxid1.setOnEditorActionListener(this);
        wxid2.setOnEditorActionListener(this);
        wxks1.setOnEditorActionListener(this);
        wxks2.setOnEditorActionListener(this);

        goHome = contentView.findViewById(R.id.goHome);
        goHome.setOnClickListener(this);
        readSetting = contentView.findViewById(R.id.readSetting);
        readSetting.setOnClickListener(this);
        writeSetting = contentView.findViewById(R.id.writeSetting);
        writeSetting.setOnClickListener(this);
        writeId = contentView.findViewById(R.id.writeId);
        writeId.setOnClickListener(this);

        return contentView;
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(buttonView.getId() == R.id.dhlyc){
            if(isChecked){
                manager.zYSystemBar(0);
            }else{
                manager.zYSystemBar(1);
            }
        }else if(buttonView.getId() == R.id.waterType){
//            String deviceKernelInfo = manager.zYgetDeviceKernelInfo();
            hold.setWaterType(isChecked ? 1 : 0);
            Toast.makeText(getActivity(),"水泵控制模式已切换",Toast.LENGTH_LONG).show();
        }else if(buttonView.getId() == R.id.reset){
            manager.zYRebootSys();
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if(!TextUtils.isEmpty(s)){
            int num = Integer.parseInt(s.toString());
            if(num > 0 && num < 255){
                manager.zYsetBackLight(num);
            }
        }
    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.goHome) {
            onActivityInteractionListener.bakeFragment();
        }else if(view.getId() == R.id.writeId){
            String idTxt = id.getText().toString();
            if(TextUtils.isEmpty(idTxt)){
                Toast.makeText(getActivity(),"请填写全部参数",Toast.LENGTH_LONG).show();
                return;
            }
            hold.setId(Short.parseShort(idTxt));
            if(onActivityInteractionListener.setHold(hold)){
                Toast.makeText(getActivity(),"本机设置完成",Toast.LENGTH_LONG).show();
            }
        }else if(view.getId() == R.id.readSetting){
            //发送查询
            if(onActivityInteractionListener.sendCommand(0x03,0,11,null)){
                Toast.makeText(getActivity(),"读取指令已发送",Toast.LENGTH_LONG).show();
            }
        }else if(view.getId() == R.id.writeSetting){

           String baudTxt = baud.getText().toString();
           String versionTxt = version.getText().toString();
           String mnl1Txt = mnl1.getText().toString();
           String mnl2Txt = mnl2.getText().toString();
           String wxxd1Txt = wxxd1.getText().toString();
           String wxxd2Txt = wxxd2.getText().toString();
           String wxid1Txt = wxid1.getText().toString();
           String wxid2Txt = wxid2.getText().toString();
           String wxks1Txt = wxks1.getText().toString();
           String wxks2Txt = wxks2.getText().toString();


            if(TextUtils.isEmpty(baudTxt)){
                Toast.makeText(getActivity(),"请填写全部参数",Toast.LENGTH_LONG).show();
                return;
            }
            if(TextUtils.isEmpty(versionTxt)){
                Toast.makeText(getActivity(),"请填写全部参数",Toast.LENGTH_LONG).show();
                return;
            }
            if(TextUtils.isEmpty(mnl1Txt)){
                Toast.makeText(getActivity(),"请填写全部参数",Toast.LENGTH_LONG).show();
                return;
            }
            if(TextUtils.isEmpty(mnl2Txt)){
                Toast.makeText(getActivity(),"请填写全部参数",Toast.LENGTH_LONG).show();
                return;
            }
            if(TextUtils.isEmpty(wxxd1Txt)){
                Toast.makeText(getActivity(),"请填写全部参数",Toast.LENGTH_LONG).show();
                return;
            }
            if(TextUtils.isEmpty(wxxd2Txt)){
                Toast.makeText(getActivity(),"请填写全部参数",Toast.LENGTH_LONG).show();
                return;
            }
            if(TextUtils.isEmpty(wxid1Txt)){
                Toast.makeText(getActivity(),"请填写全部参数",Toast.LENGTH_LONG).show();
                return;
            }
            if(TextUtils.isEmpty(wxid2Txt)){
                Toast.makeText(getActivity(),"请填写全部参数",Toast.LENGTH_LONG).show();
                return;
            }
            if(TextUtils.isEmpty(wxks1Txt)){
                Toast.makeText(getActivity(),"请填写全部参数",Toast.LENGTH_LONG).show();
                return;
            }
            if(TextUtils.isEmpty(wxks2Txt)){
                Toast.makeText(getActivity(),"请填写全部参数",Toast.LENGTH_LONG).show();
                return;
            }




            byte[] data = new byte[20];

            data[0] = 0;
            data[1] = (byte) (Integer.parseInt(baudTxt) == 115200 ? 0 : 1);

            data[2] = (byte) (Short.parseShort(versionTxt) >> 8);
            data[3] = (byte) Short.parseShort(versionTxt);

            data[4] = 0;
            data[5] = (byte) Short.parseShort(mnl1Txt);

            data[6] = 0;
            data[7] = (byte) Short.parseShort(mnl2Txt);

            data[8] = 0;
            data[9] = (byte) Short.parseShort(wxid1Txt);

            data[10] = 0;
            data[11] = (byte) Short.parseShort(wxid2Txt);

            data[12] = 0;
            data[13] = (byte) Short.parseShort(wxxd1Txt);

            data[14] =0;
            data[15] = (byte) Short.parseShort(wxxd2Txt);

            data[16] = 0;
            data[17] = (byte) Short.parseShort(wxks1Txt);

            data[18] = 0;
            data[19] = (byte) Short.parseShort(wxks2Txt);

            //发送设置
            if(onActivityInteractionListener.sendCommand(0x10,1,10,data)){
                Toast.makeText(getActivity(),"设置已发送，请读取确认",Toast.LENGTH_LONG).show();
            }
        }
    }


    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            // 关闭键盘
            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            return true;
        }
        return false;
    }
}